<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="joda" uri="http://www.joda.org/joda/time/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

<t:masterpage>
	<jsp:body>
        <c:url value="/donations" var="donationsUrl" />
        <c:url value="/staff" var="staffUrl" />
        <c:url value="/static/img/photo" var="imageFolderUrl" /> 
        <h2>Profile</h2>
        <table cellpadding="10">
        <tr>
        <td valign="top">
        <img class="photo"
			alt="${donationsPageModel.operation.receiptor.firstName} ${donationsPageModel.operation.receiptor.lastName}"
			src="${imageFolderUrl}/${donationsPageModel.operation.receiptor.photoPath}" />
        </td>
        <td valign="top" width="350">
        	<dl>
        	<dt>Name:</dt>
	        <dd>${donationsPageModel.operation.receiptor.firstName} ${donationsPageModel.operation.receiptor.lastName}</dd>        
	        <dt>Birthday:</dt>
			<dd><joda:format value="${donationsPageModel.operation.receiptor.birthday}" pattern="dd.MM.yyyy" /></dd>
			
			<c:choose>
       			<c:when test="${!donationsPageModel.operation.receiptor.fired}">
	        		<dt>Position:</dt>
	        		<dd>${donationsPageModel.operation.receiptor.position.name}</dd>
	        		<dt>Department:</dt>
	        		<dd>${donationsPageModel.operation.receiptor.department.name}</dd>
	        	</c:when>
	    		<c:otherwise>
	   				<dt>Status:</dt>
	        		<dd>fired (ex. ${donationsPageModel.operation.receiptor.position.name} in ${donationsPageModel.operation.receiptor.department.name})</dd>
	 			</c:otherwise>
			</c:choose>

	        <br/>
			<dt>Total received:</dt>
	        <dd>${donationsPageModel.totalAmount}</dd>
	        <dt>Total donators:</dt>
	        <dd>${fn:length(donationsPageModel.donators)}</dd>
	        </dl>
	        <a href="${staffUrl}/${donationsPageModel.operation.receiptor.id}/update.html">Edit profile</a>
        </td>
        </table>
        
        <h2>Donations (${donationsPageModel.operation.year})</h2>
        
        <c:choose>
        <c:when test="${!donationsPageModel.operation.receiptor.fired}">
	        <div>
	        <form:form method="post" class="form-actions"
				action="${donationsUrl}/${donationsPageModel.operation.receiptor.id}/add.html"
				modelAttribute="donationsPageModel.operation">
	            <form:hidden path="id" />
	            <form:hidden path="receiptor.id" />
	            <form:hidden path="humanResources.id" />
	            <form:hidden path="year" />
	            <table>
	                <tr>
	                    <td valign="top" align="left"><form:label path="donator.id">Donator</form:label></td>
	                    <td valign="top" align="left"><form:label path="amount">Amount</form:label></td>
	                    <td></td>
	                </tr>
	                <tr>
	                    <td valign="top">
		                    <form:select path="donator.id">
		                    	<c:forEach var="person" items="${donationsPageModel.allPeople}">
		                            <form:option value="${person.id}">
		                                <c:out value="${person.firstName} ${person.lastName}" />
		                            </form:option>
		                        </c:forEach>
		                    </form:select><br/>
		                    <form:errors path="donator.id" />
		                </td>
	                    <td valign="top">
	                    	<form:input path="amount"/><br/>
	                    	<form:errors class="text-error" path="amount" />
	                    </td>
	                    <td colspan="3" valign="top">
	                    	<input class="btn btn-primary" type="submit" value="Donate" />
	                    </td>
	                </tr>
	            </table>
	        </form:form>
	        </div>
        </c:when>
	    	<c:otherwise>
	    		<form:form class="form-actions">
	    		Donations are closed for fired persons
	    		</form:form>
	 		</c:otherwise>
		</c:choose>
        
        <c:choose>
        <c:when
			test="${donationsPageModel.donators != null && !empty donationsPageModel.donators}">
            <table id="staff" class="table table-hover">
                <thead>
                    <tr>
                        <th width="100">Donator</th>
                        <th></th>
                        <th>Amount</th>
                        <th>Action</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach items="${donationsPageModel.donators}"
						var="person">
                        <tr>
                            <td>
                            	<a href="${donationsUrl}/${person.id}.html">
                                <img class="photo_small"
								    alt="${person.firstName} ${person.lastName}"
								    src="${imageFolderUrl}/${person.photoPath}" />
								</a>
                            </td>
                            <td>
                     	       <a href="${donationsUrl}/${person.id}.html"> ${person.firstName} ${person.lastName} </a>
                            </td>
                            <td>
                            	${person.amount}
                            </td>
                            <td>
                            	<a class="btn" href="${donationsUrl}/${donationsPageModel.operation.receiptor.id}/moneyback/${person.id}.html">Money back</a>
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table> 
        </c:when>
	    <c:otherwise>
	   		<p class="empty">Nobody has donated ${donationsPageModel.operation.receiptor.firstName} yet :(</p>
	 	</c:otherwise>
		</c:choose>
        
        <hr/>
        
        
       
    </jsp:body>
</t:masterpage>